package cryptography;

/**
 *
 * @author Mouchicc & Mavs
 * @version 1.0
 */
public class Caesar {

    final String alfabet = "abcdefghijklmnopqrstuvwxyz";
    final String ALFABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    String ENCRYPT = null;
    int KEY = -1;

    public Caesar(String e) {
        this.ENCRYPT = e;
    }

    public Caesar(String e, int k) {
        this.ENCRYPT = e;
        this.KEY = k;
    }

    public void setKey(int k) {
        this.KEY = k;
    }

    public void setEncrypt(String s) {
        this.ENCRYPT = s;
    }

    public String encrypt() {
        String res = "";
        String alf;
        if (ENCRYPT == null || ENCRYPT.length() == 0) {
            return null;
        }

        for (int i = 0; i < ENCRYPT.length(); i++) {
            if (ENCRYPT.charAt(i) == '\n' && ENCRYPT.charAt(i) != ' ') {
                res += '\n';
            } else if (ENCRYPT.charAt(i) != '\n' && ENCRYPT.charAt(i) == ' ') {
                res += ' ';
            } else if (ENCRYPT.charAt(i) == '.') {
                res += '.';
            } else {
                if (Character.isUpperCase(ENCRYPT.charAt(i))) {
                    alf = ALFABET;
                } else {
                    alf = alfabet;
                }
                res += alf.charAt(Methods.mod((Methods.charToNum(ENCRYPT.charAt(i)) + KEY), 26));
            }
        }
        return res;
    }

    public String decrypt() {
        String res = "";
        String alf;
        if (ENCRYPT == null || ENCRYPT.length() == 0) {
            return null;
        }

        for (int i = 0; i < ENCRYPT.length(); i++) {
            if (ENCRYPT.charAt(i) == '\n' && ENCRYPT.charAt(i) != ' ') {
                res += '\n';
            } else if (ENCRYPT.charAt(i) != '\n' && ENCRYPT.charAt(i) == ' ') {
                res += ' ';
            } else if (ENCRYPT.charAt(i) == '.') {
                res += '.';
            } else {
                if (Character.isUpperCase(ENCRYPT.charAt(i))) {
                    alf = ALFABET;
                } else {
                    alf = alfabet;
                }
                res += alf.charAt(Methods.mod(
                        (Methods.charToNum(ENCRYPT.charAt(i)) - KEY), 26));
            }
        }
        return res;
    }
}
